package com.hong.qrpc.strategy.client.config;

import com.hong.config.constant.ClientConfig;
import com.hong.qrpc.RpcClient;
import com.hong.qrpc.codec.impl.HessianDecoder;
import com.hong.qrpc.codec.impl.HessianEncoder;
import com.hong.qrpc.codec.impl.JSONDecoder;
import com.hong.qrpc.codec.impl.JSONEncoder;
import com.hong.qrpc.config.ConfigManager;
import com.hong.qrpc.strategy.PropertiesStrategy;
import com.hong.qrpc.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @Author HQT
 * @Date 2022/7/17 14:41
 */
@Slf4j
public class CodeClassStrategy implements PropertiesStrategy {
    @Override
    public void disposeProperties(RpcClient rpcClient) {
        ConfigManager configManager = rpcClient.getConfigManager();
        Map<Object, Object> profile = configManager.getProfile();
        String manner = (String) profile.get(ClientConfig.QRPC_CODE_MANNER);
        if (!StringUtils.isEmpty(manner)) {
            switch (manner.toLowerCase()) {
                case "json":
                    configManager.getRpcClientConfig().setDecoderClass(JSONDecoder.class);
                    configManager.getRpcClientConfig().setEncoderClass(JSONEncoder.class);
                    return;
                case "hessian":
                    configManager.getRpcClientConfig().setDecoderClass(HessianDecoder.class);
                    configManager.getRpcClientConfig().setEncoderClass(HessianEncoder.class);
                    return;
                default:
                    log.warn("unsupported decoding manner {}", manner);
            }
        }
    }
}
